package roadsuggester.db;

import java.util.ArrayList;
import java.util.List;

import roadsuggester.db.UserLocationsDatabase.UserLocation;
import roadsuggester.tracks.NavigationTrackOverlay;
import roadsuggester.tracks.RecordedTrack;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
import android.location.Location;


public class UserTracksDatabase extends SQLiteOpenHelper
{
	private static final int DATABASE_VERSION=1;
	private static final String TABLE_NAME="UserTracks";
	private SQLiteDatabase db;

	private static List<RecordedTrack> tracks=null; //In-memory database TODO: delete this

	public UserTracksDatabase(Context context)
	{
		super(context, "tracks", null, DATABASE_VERSION);


	}

	@Override
	public void onCreate(SQLiteDatabase db)
	{
		db.execSQL("CREATE TABLE UserTracks("+"_id INTEGER PRIMARY KEY AUTOINCREMENT"+"startLattitude FLOAT,"+"startLongitude FLOAT,"
				+"destinationLattitude FLOAT"+"destinationLongitude FLOAT"+"timeTook INTEGER);");
		db.execSQL("CREATE INDEX coordsIndex ON UserTracks(startLattitude, startLongitude, destinationLattitude, destinationLongitude)");


	}

	@Override
	public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2)
	{


	}

	public void addTrack(RecordedTrack recordedTrack)
	{
		
		// TODO: make this a sqlite database
		if (tracks==null)
			tracks=new ArrayList<RecordedTrack>();
		tracks.add(recordedTrack);

	}

	public RecordedTrack getFastestNavigationTrack(Location from, Location to)
	{
		// TODO: make this a sqlite database
		if (tracks==null)
			tracks=new ArrayList<RecordedTrack>();
		if(tracks.size()==0)
		{
			return null;
		}
		RecordedTrack fastestTrack=tracks.get(0);
		for(int i=0; i<tracks.size(); i++)
		{
			if(tracks.get(i).timeTook()<fastestTrack.timeTook())
			{
				fastestTrack=tracks.get(i);
			}
		}

		return fastestTrack;
	}
}
